package com.prs.crm.dao.cqunicom.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cqunicom.OrderDertyHistoryDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cqunicom.OrderDertyHistory;

public class OrderDertyHistoryDaoImpl extends BaseDaoImpl<OrderDertyHistory>
		implements OrderDertyHistoryDao {

	@SuppressWarnings("unchecked")
	@Override
	public OrderDertyHistory getByOrderId(final String ids) {
		return (OrderDertyHistory) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = arg0
								.createCriteria(OrderDertyHistory.class);
						criteria.add(Restrictions.eq("objectId", ids));
						List<OrderDertyHistory> ss = criteria.list();
						if (ss.size() == 0) {
							return null;
						} else {
							return ss.get(0);
						}

					}

				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderDertyHistory> getByMaxOrderId(final String ids) {
		return (List<OrderDertyHistory>) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = arg0.createCriteria(OrderDertyHistory.class);
//						criteria.setMaxResults(0);
						criteria.add(Restrictions.eq("objectId", ids));
						return criteria.list();

					}

				});
	}

	@Override
	public OrderDertyHistory getByaddOrderId(final int ids) {
		return (OrderDertyHistory) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = arg0
								.createCriteria(OrderDertyHistory.class);
						criteria.add(Restrictions.eq("recordNum", ids-1));
						List<OrderDertyHistory> ss = criteria.list();
						if (ss.size() == 0) {
							return null;
						} else {
							return ss.get(0);
						}

					}

				});
	}

}
